* Importing packages
* ssc install estout
* ssc install ftools
* ssc install reghdfe
* ssc install cibar
* ssc install rev
* ssc install sutex
* ssc install cibar
* ssc install ipfweight
* ssc install ciplot

**************************************************************************************************
*****************A) Germany dataset.  ************************************************************
**************************************************************************************************


cd "[insert your directory]"

use "DE_stage1_2.dta", clear

global PathTab "[insert your directory]"
global PathFig "[insert your directory]"


* Recoding variables
keep if partychoice!=.
gen right_DE = .
replace right_DE = 1 if partychoice==1 | partychoice==4 | partychoice==6
replace right_DE = 0 if partychoice==2 | partychoice==3 | partychoice==5


*** Other 
recode education (1 2 3 4 6 7=0) (8 9 10 =1) (else=.), gen(postsecondary_DE) 

recode gender (1=1) (2=0) (else=.), gen(gender_cat) 
gen age_real = 2024-age
recode age_real (18/34=1) (35/54=2) (55/90=3), gen(age_cat)
tab gender_cat, gen(gender_cat)
tab age_cat, gen(age_cat)


label var gender_cat1 "Female"
label var age_cat1 "Age: 18-34"
label var age_cat2 "Age: 35-54"
label var age_cat3 "Age: 55+"
label var postsecondary_DE "Post-secondary educ."
label var right_DE "Right-wing party"

* NUTS1
gen region_DE1=0
replace region_DE1=1 if pref==1
gen region_DE2=0
replace region_DE2=1 if pref==2
gen region_DE3=0
replace region_DE3=1 if pref==3
gen region_DE4=0
replace region_DE4=1 if pref==4
gen region_DE5=0
replace region_DE5=1 if pref==5
gen region_DE6=0
replace region_DE6=1 if pref==6
gen region_DE7=0
replace region_DE7=1 if pref==7
gen region_DE8=0
replace region_DE8=1 if pref==8
gen region_DE9=0
replace region_DE9=1 if pref==9
gen region_DE10=0
replace region_DE10=1 if pref==10
gen region_DE11=0
replace region_DE11=1 if pref==11
gen region_DE12=0
replace region_DE12=1 if pref==12
gen region_DE13=0
replace region_DE13=1 if pref==13
gen region_DE14=0
replace region_DE14=1 if pref==14
gen region_DE15=0
replace region_DE15=1 if pref==15
gen region_DE16=0
replace region_DE16=1 if pref==16

label var region_DE1 "Baden-Württemberg"
label var region_DE2 "Bayern"
label var region_DE3 "Berlin"
label var region_DE4 "Brandenburg"

label var region_DE5 "Bremen"
label var region_DE6 "Hamburg"
label var region_DE7 "Hessen"
label var region_DE8 "Mecklenburg-Vorpommern"

label var region_DE9 "Niedersachsen"
label var region_DE10 "Nordrhein-Westfalen"
label var region_DE11 "Rheinland-Pfalz"
label var region_DE12 "Saarland"

label var region_DE13 "Sachsen"
label var region_DE14 "Sachsen-Anhalt"
label var region_DE15 "Schleswig-Holstein"
label var region_DE16 "Thüringen"

** Table S6

sutex gender_cat1 age_cat1 age_cat2 age_cat3 postsecondary_DE region_DE1-region_DE16 right_DE , minmax labels title("Summary statistics, German sample, Stages 1-2") file("${PathTab}TableS6.tex") replace



lab def approve 0 "0 Disapprove" 10 "10 Approve"
foreach var in contributing_1 contributing_2 obeying_1 accepting_3 {
recode `var' 99="."
lab values `var' approve
}
foreach var of varlist contributing_1 contributing_2 obeying_1 accepting_3{
	rev `var', gen(`var'_rev) 
}

label variable contributing_1_rev "Welfare norm violation"
label variable contributing_2_rev "Abstention"
label variable obeying_1_rev "Corruption"
label variable accepting_3_rev "Gender equity violation"

gen d_contributing_1_mus_gr = contributing_1_1 - contributing_1_2
gen d_accepting_3_mus_gr =  accepting_3_1 - accepting_3_2
rename d_contributing_1_mus_gr d_contributing_1_mus_nat
rename d_accepting_3_mus_gr d_accepting_3_mus_nat

label variable contributing_1_1 "Welfare norm violation (natives)"
label variable contributing_1_2 "Welfare norm violation (immigrants)"
label variable accepting_3_1 "Gender equity violation (natives)"
label variable accepting_3_2 "Gender equity violation (immigrants)"
label variable d_contributing_1_mus_nat "Welfare norm violation (difference natives vs immigrants)"
label variable d_accepting_3_mus_nat "Gender equity violation (difference natives vs immigrants)"




save "DE_recoded_stage1_2.dta", replace




**************************************************************************************************
*****************B) Greece dataset.   ************************************************************
**************************************************************************************************


cd "[insert your directory]"

use "GR_stage1_2.dta", clear

global PathTab "[insert your directory]"
global PathFig "[insert your directory]"


* Recoding key variables
keep if partychoice!=.
gen right_GR=0
replace right_GR = 1 if partychoice==1 | partychoice==5 | partychoice==6 | partychoice==7  | partychoice==10
replace right_GR=. if partychoice==11 

recode gender (1=1) (2=2) (else=.), gen(gender_cat) 
recode age (18/39=1) (40/54=2) (55/90=3) (.2 = .), gen(age_cat)
tab gender_cat, gen(gender_cat)
tab age_cat, gen(age_cat)
gen postsecondary_GR=1
replace postsecondary_GR=0 if education==1 | education==2 | education==3 | education==4 | education==9
replace postsecondary_GR=. if education==11

gen perifereia=""
replace perifereia="ipeirou" if prefecture_1==7
replace perifereia="anatol_mak" if prefecture_1==14
replace perifereia="dyt_ell" if prefecture_1==3
replace perifereia="peloponnisou" if prefecture_1==5
replace perifereia="peloponnisou" if prefecture_1==6
replace perifereia="attikis" if prefecture_1==4 | prefecture_1==2 | prefecture_1==12 | prefecture_1==41
replace perifereia="dyt_ell" if prefecture_1==8
replace perifereia="stereas" if prefecture_1==9
replace perifereia="dytikis_mak" if prefecture_1==10
replace perifereia="anatol_mak" if prefecture_1==11
replace perifereia="notiou_aig" if prefecture_1==13
replace perifereia="stereas" if prefecture_1==16
replace perifereia="stereas" if prefecture_1==15
replace perifereia="ionion" if prefecture_1==17
replace perifereia="dyt_ell" if prefecture_1==18
replace perifereia="kentr_mak" if prefecture_1==19
replace perifereia="kritis" if prefecture_1==20
replace perifereia="ipeirou" if prefecture_1==21
replace perifereia="kentr_mak" if prefecture_1==22
replace perifereia="ipeirou" if prefecture_1==23
replace perifereia="ionion" if prefecture_1==27
replace perifereia="anatol_mak" if prefecture_1==24
replace perifereia="thessalias" if prefecture_1==25
replace perifereia="dytikis_mak" if prefecture_1==26
replace perifereia="ionion" if prefecture_1==28
replace perifereia="kentr_mak" if prefecture_1==29
replace perifereia="dytikis_mak" if prefecture_1==30
replace perifereia="peloponnisou" if prefecture_1==31
replace perifereia="notiou_aig" if prefecture_1==32
replace perifereia="thessalias" if prefecture_1==34
replace perifereia="voreiou_aig" if prefecture_1==36
replace perifereia="peloponnisou" if prefecture_1==33
replace perifereia="kritis" if prefecture_1==35
replace perifereia="thessalias" if prefecture_1==38
replace perifereia="anatol_mak" if prefecture_1==40
replace perifereia="kentr_mak" if prefecture_1==42
replace perifereia="kentr_mak" if prefecture_1==43
replace perifereia="ipeirou" if prefecture_1==44
replace perifereia="kritis" if prefecture_1==45
replace perifereia="anatol_mak" if prefecture_1==46
replace perifereia="voreiou_aig" if prefecture_1==47
replace perifereia="kentr_mak" if prefecture_1==48
replace perifereia="thessalias" if prefecture_1==49
replace perifereia="stereas" if prefecture_1==50
replace perifereia="dytikis_mak" if prefecture_1==51
replace perifereia="stereas" if prefecture_1==52
replace perifereia="voreiou_aig" if prefecture_1==55
replace perifereia="kentr_mak" if prefecture_1==53
replace perifereia="kritis" if prefecture_1==54
replace perifereia="ionion" if prefecture_1==37
replace perifereia="peloponnisou" if prefecture_1==39

gen NUTS1_string=""
replace NUTS1_string="Attiki" if perifereia=="attikis"
replace NUTS1_string="Voreia Ellada" if perifereia=="anatol_mak" | perifereia=="kentr_mak" | perifereia=="dytikis_mak" | perifereia=="ipeirou" 
replace NUTS1_string="Kentriki Ellada" if perifereia=="thessalias" | perifereia=="ionion" | perifereia=="dyt_ell" | perifereia=="stereas" | perifereia=="peloponnisou"
replace NUTS1_string="Nisia Aigaiou and Kriti" if perifereia=="voreiou_aig" | perifereia=="notiou_aig" | perifereia=="kritis"

gen region=.
replace region=1 if NUTS1_string=="Attiki"
replace region=2 if NUTS1_string=="Voreia Ellada"
replace region=3 if NUTS1_string=="Kentriki Ellada"
replace region=4 if NUTS1_string=="Nisia Aigaiou and Kriti"
tab region, gen(region_GR)
label variable region_GR1	"Attiki"
label variable region_GR2	"Voreia Ellada"
label variable region_GR3	"Kentriki Ellada"
label variable region_GR4	"Nisia Aigaiou and Kriti"

label var gender_cat1 "Male"
label var age_cat1 "Age: 18-39"
label var age_cat2 "Age: 40-54"
label var age_cat3 "Age: 55+"
label var postsecondary_GR "Post-secondary educ."
label var right_GR "Right-wing party"


** Table S8

sutex gender_cat1  age_cat1 age_cat2 age_cat3  postsecondary_GR region_GR1 region_GR2 region_GR3 region_GR4  right_GR , lab nobs key(descstat) replace title("Summary statistics, Greek sample, Stages 1-2") file("${PathTab}TableS8.tex")


lab def approve 0 "0 Disapprove" 10 "10 Approve"
foreach var in contributing_1 contributing_2 obeying_1 accepting_3 {
recode `var' 99="."
lab values `var' approve
}
foreach var of varlist contributing_1 contributing_2 obeying_1 accepting_3{
	rev `var', gen(`var'_rev) 
}

label variable contributing_1_rev "Welfare norm violation"
label variable contributing_2_rev "Abstention"
label variable obeying_1_rev "Corruption"
label variable accepting_3_rev "Gender equity violation"


gen d_contributing_1_syr_gr = contributing_1_syr_1 - contributing_1_syr_2
gen d_accepting_3_syr_gr =  accepting_3_syr_1 - accepting_3_syr_2

rename	contributing_1_syr_1 contributing_1_1
rename	contributing_1_syr_2 contributing_1_2
rename	accepting_3_syr_1 accepting_3_1
rename	accepting_3_syr_2 accepting_3_2

rename d_contributing_1_syr_gr d_contributing_1_mus_nat
rename d_accepting_3_syr_gr d_accepting_3_mus_nat

label variable contributing_1_1 "Welfare norm violation (natives)"
label variable contributing_1_2 "Welfare norm violation (immigrants)"
label variable accepting_3_1 "Gender equity violation (natives)"
label variable accepting_3_2 "Gender equity violation (immigrants)"
label variable d_contributing_1_mus_nat "Welfare norm violation (difference natives vs immigrants)"
label variable d_accepting_3_mus_nat "Gender equity violation (difference natives vs immigrants)"


save "GR_recoded_stage1_2.dta", replace






**************************************************************************************************
*****************C) Poland dataset.   ************************************************************
**************************************************************************************************


cd "[insert your directory]"

use "PL_stage1_2.dta", clear

global PathTab "[insert your directory]"
global PathFig "[insert your directory]"

recode sex 2=0 3=., gen(female) 
gen R_age=2023-year
gen postsecondary_PL = 1 if education>4
replace postsecondary_PL = 0 if education<5
replace postsecondary_PL = . if education==8 
ta woj, gen(region_PL)
label variable R_age "Age"
label variable female "Female"
label variable postsecondary_PL "Post-secondary educ."
label variable region_PL1	"dolnośląskie"
label variable region_PL2	"kujawsko-pomorskie"
label variable region_PL3	"lubelskie"
label variable region_PL4	"lubuskie"
label variable region_PL5	"łódzkie"
label variable region_PL6	"małopolskie"
label variable region_PL7	"mazowieckie"
label variable region_PL8	"opolskie"
label variable region_PL9	"podkarpackie"
label variable region_PL10	"podlaskie"
label variable region_PL11	"pomorskie"
label variable region_PL12	"śląskie"
label variable region_PL13	"świętokrzyskie"
label variable region_PL14	"warmińsko-mazurskie"
label variable region_PL15	"wielkopolskie"
label variable region_PL16	"zachodniopomorskie"
recode R_age (18/34 = 1 "Age 18-34") (35/54 = 2 "Age 35/54") (55/90 = 3 "Age 55/90"), gen(age_cat_new)
tab age_cat_new, gen(age_cat_new)
label variable age_cat_new1 "Age 18-34"
label variable age_cat_new2 "Age 35/54"
label variable age_cat_new3 "Age 55/90"
gen right_PL = 1 if partychoice==1 | partychoice==5 | partychoice==7 | partychoice==8 | partychoice==11 | partychoice==3 | partychoice==6
replace right_PL = 0 if partychoice!=1 & partychoice!=5 & partychoice!=7 & partychoice!=8 & partychoice!=11 & partychoice!=3 & partychoice!=6
label variable right_PL "Right-wing"


** Table S10

sutex female age_cat_new1 age_cat_new2 age_cat_new3  postsecondary_PL region_PL1-region_PL16 right_PL , minmax labels title("Summary statistics, Polish sample, Stages 1-2") file("${PathTab}TableS10.tex") replace


lab def approve 0 "0 Disapprove" 10 "10 Approve"
foreach var of varlist contributing_r1 contributing_r2 obeying_r1 accepting_r3{
	recode `var' 99="."
	rev `var', gen(`var'_rev) 
}

foreach var in contributing_1_ind_pl contributing_1_ind_ind contributing_2_ind_pl contributing_2_ind_ind contributing_1_afg_pl contributing_1_afg_afg accepting_3_ind_pl accepting_3_ind_ind accepting_3_afg_pl accepting_3_afg_afg {
recode `var' 999="."
}

rename	contributing_r1_rev contributing_1_rev
rename	contributing_r2_rev contributing_2_rev
rename	obeying_r1_rev obeying_1_rev
rename	accepting_r3_rev accepting_3_rev

label variable contributing_1_rev "Welfare norm violation"
label variable contributing_2_rev "Abstention"
label variable obeying_1_rev "Corruption"
label variable accepting_3_rev "Gender equity violation"

gen d_contributing_1_mus_nat = contributing_1_ind_pl - contributing_1_ind_ind
gen d_accepting_3_mus_nat =  accepting_3_ind_pl - accepting_3_ind_ind

rename	contributing_1_ind_pl contributing_1_1
rename	contributing_1_ind_ind contributing_1_2
rename	accepting_3_ind_pl accepting_3_1
rename	accepting_3_ind_ind accepting_3_2

label variable contributing_1_1 "Welfare norm violation (natives)"
label variable contributing_1_2 "Welfare norm violation (immigrants)"
label variable accepting_3_1 "Gender equity violation (natives)"
label variable accepting_3_2 "Gender equity violation (immigrants)"
label variable d_contributing_1_mus_nat "Welfare norm violation (difference natives vs immigrants)"
label variable d_accepting_3_mus_nat "Gender equity violation (difference natives vs immigrants)"




save "PL_recoded_stage1_2.dta", replace





**************************************************************************************************
*****************D) MERGE datasets.   ************************************************************
**************************************************************************************************


cd "[insert your directory]"


global PathTab "[insert your directory]"
global PathFig "[insert your directory]"


use "DE_recoded_stage1_2.dta", clear

gen country = "Germany"

append using "GR_recoded_stage1_2.dta", force

replace country = "Greece" if missing(country)


append using "PL_recoded_stage1_2.dta", force

replace country = "Poland" if missing(country)

label variable country "Sample"


** Figure 4

ciplot contributing_2_rev obeying_1_rev contributing_1_rev accepting_3_rev, by(country) note("") xlabel(, angle(0)) rcap(lcolor(black)) name(injunct, replace) yline(5, lcolor(gray)) ytitle("Disapproval (0 - 10)") ylabel(4(1)8) legend(rows(2) position(bottom)) xline(9.5, lpattern(solid)) xline(19, lpattern(solid)) xtitle("") msymbol(0 T S D) aspectratio(.8)
graph export "${PathFig}Figure4.eps", as(eps) replace


** Table S21

bys country: sum contributing_1_rev contributing_2_rev obeying_1_rev accepting_3_rev 



** Figure 5

label variable contributing_1_1 "Natives: Welfare norm"
label variable contributing_1_2 "Immigrants: Welfare norm"
label variable accepting_3_1 "Natives: Gender equity norm"
label variable accepting_3_2 "Immigrants: Gender equity norm"

ciplot contributing_1_1   contributing_1_2 accepting_3_1 accepting_3_2, by(country) note("") xlabel(, angle(0)) rcap(lcolor(black)) name(injunct, replace) yline(5, lcolor(gray)) ytitle("Likelihood of norm violation (%)") ylabel(30(5)70)  legend(rows(2) position(bottom)) xline(9.5, lpattern(solid)) xline(19, lpattern(solid)) xtitle("") mcolor(black pink black pink) msymbol(S S D D) aspectratio(.8)
graph export "${PathFig}Figure5.eps", as(eps) replace


** Table S22

bys country: sum contributing_1_1   contributing_1_2 accepting_3_1 accepting_3_2 d_contributing_1_mus_nat d_accepting_3_mus_nat 


** Figure S7

* SYR vs NIG
label variable contributing_1_1 "Natives: Welfare norm"
label variable contributing_1_nig_1 "Natives: Welfare norm"
label variable contributing_1_2 "Syrians: Welfare norm"
label variable contributing_1_nig_2 "Nigerians: Welfare norm"
label variable accepting_3_nig_1 "Natives: Gender equity norm"
label variable accepting_3_1 "Natives: Gender equity norm"
label variable accepting_3_2 "Syrians: Gender equity norm"
label variable accepting_3_nig_2 "Nigerians: Gender equity norm"

ciplot contributing_1_1   contributing_1_2  accepting_3_1 accepting_3_2  if country=="Greece", xline(5) note("") xlabel(, angle(45)) rcap(lcolor(black)) name(beliefs1, replace)  ytitle("Likelihood of norm violation (%)") ylabel(30(5)70)  legend(rows(2) position(bottom)) xtitle("Natives vs Syrians") mcolor(black pink black pink) msymbol(S S D D) aspectratio(.8)
ciplot contributing_1_nig_1   contributing_1_nig_2  accepting_3_nig_1 accepting_3_nig_2  if country=="Greece",  xline(5) note("") xlabel(, angle(45)) rcap(lcolor(black)) name(beliefs2, replace)  ytitle("Likelihood of norm violation (%)") ylabel(30(5)70)  legend(rows(2) position(bottom)) xtitle("Natives vs Nigerians") mcolor(black pink black pink) msymbol(S S D D) aspectratio(.8)
gr combine beliefs1 beliefs2 , col(2) ycommon title("Greece")
graph export "${PathFig}FigureS7_GR.eps", as(eps) replace

* IND vs AFG
label variable contributing_1_afg_pl "Natives: Welfare norm"
label variable contributing_1_1 "Natives: Welfare norm"
label variable contributing_1_afg_afg "Afghans: Welfare norm"
label variable contributing_1_2 "Indians: Welfare norm"
label variable accepting_3_afg_pl "Natives: Gender equity norm"
label variable accepting_3_1 "Natives: Gender equity norm"
label variable accepting_3_afg_afg "Afghans: Gender equity norm"
label variable accepting_3_2 "Indians: Gender equity norm"

ciplot contributing_1_1   contributing_1_2  accepting_3_1 accepting_3_2  if country=="Poland", xline(5) note("") xlabel(, angle(45)) rcap(lcolor(black)) name(beliefs1, replace)  ytitle("Likelihood of norm violation (%)") ylabel(30(5)70)  legend(rows(2) position(bottom)) xtitle("Natives vs Indians") mcolor(black pink black pink) msymbol(S S D D) aspectratio(.8)
ciplot contributing_1_afg_pl   contributing_1_afg_afg  accepting_3_afg_pl accepting_3_afg_afg  if country=="Poland",  xline(5) note("") xlabel(, angle(45)) rcap(lcolor(black)) name(beliefs2, replace)  ytitle("Likelihood of norm violation (%)") ylabel(30(5)70)  legend(rows(2) position(bottom)) xtitle("Natives vs Afghans") mcolor(black pink black pink) msymbol(S S D D) aspectratio(.8)
gr combine beliefs1 beliefs2 , col(2) ycommon title("Poland")
graph export "${PathFig}FigureS7_PL.eps", as(eps) replace


** Figure S12

gen right_wing = right_DE
replace right_wing = right_GR if right_wing==.
replace right_wing = right_PL if right_wing==.

label variable contributing_1_1 "Natives: Welfare norm"
label variable contributing_1_2 "Immigrants: Welfare norm"
label variable accepting_3_1 "Natives: Gender equity norm"
label variable accepting_3_2 "Immigrants: Gender equity norm"

ciplot contributing_1_1   contributing_1_2 accepting_3_1 accepting_3_2 if right_wing==1, by(country) note("") xlabel(, angle(0)) rcap(lcolor(black)) name(injunctRW1, replace) yline(50, lcolor(gray)) ytitle("Likelihood of norm violation (%)") ylabel(30(5)70)  legend(rows(2) position(bottom)) xline(9.5, lpattern(solid)) xline(19, lpattern(solid)) xtitle("Right-wing") mcolor(black pink black pink) msymbol(S S D D) aspectratio(.8)
ciplot contributing_1_1   contributing_1_2 accepting_3_1 accepting_3_2 if right_wing==0, by(country) note("") xlabel(, angle(0)) rcap(lcolor(black)) name(injunctRW2, replace) yline(50, lcolor(gray)) ytitle("Likelihood of norm violation (%)") ylabel(30(5)70)  legend(rows(2) position(bottom)) xline(9.5, lpattern(solid)) xline(19, lpattern(solid)) xtitle("Center-left") mcolor(black pink black pink) msymbol(S S D D) aspectratio(.8)
graph combine injunctRW1 injunctRW2
graph export "${PathFig}FigureS12.eps", as(eps) replace
